<?php
namespace modules\setting\services\impl;

use modules\setting\model\SysMenu;
use modules\setting\services\SysMenuService;
use modules\utils\Tree;

class SysMenuServiceImpl implements SysMenuService
{
    public function getMenuList()
    {
        $list=SysMenu::select()->toArray();
        $tree=Tree::buildTree($list);
        return $tree;
    }
    public function addMenu(int $pid, $data)
    {
        $data['pid'] = $pid;
        return SysMenu::create($data);
    }
    public function updateMenu($id, $data)
    {
        return SysMenu::where('id', $id)->update($data);
    }
    public function deleteMenu($id)
    {
        //判断是否有子菜单
        $count=SysMenu::where('pid',$id)->count();
        if($count>0){
            throw new \think\exception\ValidateException('该菜单下有子菜单，不能删除');
        }
        return SysMenu::where('id', $id)->delete();
    }
}